package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetMealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SetMealServiceImpl implements SetMealService {
    @Autowired
    SetmealMapper setmealMapper;

    @Autowired
    SetmealDishMapper setmealDishMapper;

    @Autowired
    DishMapper dishMapper;
    /*
    * 新增套餐
    * */
    @Override
    @Transactional
    public void save(SetmealDTO setmealDTO) {
        //在套餐表插入
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.insert(setmeal);

        //获取insert语句生成的主键值
        Long setMealId = setmeal.getId();
        List<SetmealDish> setMealDishes = setmealDTO.getSetmealDishes();

        if(setMealDishes!=null&&setMealDishes.size()>0){
            setMealDishes.forEach(setmealDish -> {
                setmealDish.setSetmealId(setMealId);
            });
            setmealDishMapper.insertBatch(setMealDishes);
        }

    }

    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        //开始分页查询  页码，每页的记录数
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());

        Page<Setmeal> page = setmealMapper.pageQuery(setmealPageQueryDTO);

        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    @Transactional
    public void update(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);

        //更新套餐表
        setmealMapper.update(setmeal);

        //删除套餐菜品表
        setmealDishMapper.deleteById(setmealDTO.getId());

        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();

        if(setmealDishes!=null&&setmealDishes.size()>0){
            setmealDishes.forEach(setmealDish -> {
                setmealDish.setSetmealId(setmealDTO.getId());
            });
            //插入更新的套餐菜品表
            setmealDishMapper.insertBatch(setmealDishes);
        }

    }

    @Override
    public SetmealVO getById(Long id) {
        //获取套餐
        Setmeal setmeal = setmealMapper.getById(id);

        //获取套餐菜品
        List<SetmealDish> list = setmealDishMapper.getById(id);
        //封装数据
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal,setmealVO);
        setmealVO.setSetmealDishes(list);
        return setmealVO;
    }

    @Override
    public void startOrStop(Integer status, Long id) {
        if(status.equals(StatusConstant.ENABLE)){
            List<SetmealDish> setmealDishes = setmealDishMapper.getById(id);
            for (SetmealDish setMealDish : setmealDishes) {
                if(dishMapper.getById(setMealDish.getDishId()).getStatus().equals(StatusConstant.DISABLE)){
                    throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                }
            }
        }

        Setmeal setmeal = Setmeal.builder().
                id(id).
                status(status).
                build();
        setmealMapper.update(setmeal);
    }

    @Override
    public void delete(List<Long> ids) {
//        //判断套餐是否在售
//        for (Long id : ids) {
//            Setmeal setmeal = setmealMapper.getById(id);
//            if(setmeal.getStatus().equals(StatusConstant.ENABLE)){
//                //套餐起售中不能删除
//                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
//            }
//        }
//
//
//        //根据套餐id批量删除套餐数据
//        setmealMapper.deleteByIds(ids);
//
//        //根据菜品id删除口味数据
//        setmealDishMapper.deleteByIds(ids);
        ids.forEach(id -> {
            Setmeal setmeal = setmealMapper.getById(id);
            if(StatusConstant.ENABLE == setmeal.getStatus()){
                //起售中的套餐不能删除
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        });

        ids.forEach(setmealId -> {
            //删除套餐表中的数据
            setmealMapper.deleteById(setmealId);
            //删除套餐菜品关系表中的数据
            setmealDishMapper.deleteBySetmealId(setmealId);
        });
    }

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
